#include "administrator_info_add.h"
#include "ui_administrator_info_add.h"

administrator_info_add::administrator_info_add(QSqlTableModel *M, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::administrator_info_add)
{
    this->M = M;
    ui->setupUi(this);
    db = DBOperation::getInstance();
    db->open();

    this->setStyleSheet("QDialog{background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(251,102,102, 200), stop:1 rgba(20,196,188, 210));}");
    ui->passwordLineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                        "color: rgb(31,31,31);"		//字体颜色
                                        "padding-left:20px;"       //内边距-字体缩进
                                        "background-color: rgb(255, 255, 255);" //背景颜色
                                        "border:2px solid rgb(20,196,188);border-radius:15px;");

    ui->usernameLineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                        "color: rgb(31,31,31);"		//字体颜色
                                        "padding-left:20px;"       //内边距-字体缩进
                                        "background-color: rgb(255, 255, 255);" //背景颜色
                                        "border:2px solid rgb(20,196,188);border-radius:15px;");

    ui->savePushButton->setStyleSheet(
                "QPushButton{"
                "color: rgb(0, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(0, 0, 0);"
                "border-radius:10px;"
                "background-color: rgba(20,196,188, 210);"
                "font:36px '宋体';}"
                "QPushButton::hover{"
                "border:2px groove gray;"
                "border-color: rgb(255, 255, 255);"
                "background-color: rgba(251,102,102, 200);"
                "color:rgb(255,255,255);}"
                "QPushButton::pressed{"
                "color: rgb(255, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(255, 0, 0);"
                "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(20,196,188, 210), stop:1 rgba(251,102,102, 200));}");

    ui->cancelPushButton->setStyleSheet(
                "QPushButton{"
                "color: rgb(0, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(0, 0, 0);"
                "border-radius:10px;"
                "background-color: rgba(20,196,188, 210);"
                "font:36px '宋体';}"
                "QPushButton::hover{"
                "border:2px groove gray;"
                "border-color: rgb(255, 255, 255);"
                "background-color: rgba(251,102,102, 200);"
                "color:rgb(255,255,255);}"
                "QPushButton::pressed{"
                "color: rgb(255, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(255, 0, 0);"
                "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(20,196,188, 210), stop:1 rgba(251,102,102, 200));}");
}

administrator_info_add::~administrator_info_add()
{
    db->close();
    delete ui;
}

void administrator_info_add::on_savePushButton_clicked()
{
    QString username = ui->usernameLineEdit->text();
    QString password = ui->passwordLineEdit->text();
    QString adminType = ui->adminTypeComboBox->currentText();
    QString sql;

    if (username.isEmpty() || password.isEmpty()) {
        QMessageBox::warning(this, "警告", "请输入必填内容");
    }
    else {
        sql = "select * from admin;";
        db->operate(sql);
        bool flag = true;
        while (db->getNext()) {
            if (db->getValue("username") == username) {
                flag = false;
                QMessageBox::warning(this, "提示", "该管理员已存在");
                break;
            }
        }
        if (flag) {
            sql = QString("INSERT INTO admin VALUES(NULL, '%1', '%2', '%3');").arg(username, password, adminType);
            db->operate(sql);
            QMessageBox::information(this, "提示", "添加成功");
            on_cancelPushButton_clicked();
        }
    }
}

void administrator_info_add::on_cancelPushButton_clicked()
{
    M->select();
    this->close();
}

